/**
 * file:GridBuildingInfoHandler
 */
package com.hongguan.oms.web.grid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.servlet.ModelAndView;

import com.hongguan.oms.common.ConstGrid;
import com.hongguan.oms.domain.UserInfo;
import com.hongguan.oms.domain.grid.GridAuthInfo;
import com.hongguan.oms.domain.grid.GridGroupCustInfo;
import com.hongguan.oms.service.gis.GisService;
import com.hongguan.oms.service.grid.GridAddressInfoService;
import com.hongguan.oms.service.grid.GridAreasService;
import com.hongguan.oms.service.grid.GridAuthInfoService;
import com.hongguan.oms.service.grid.GridCommunityInfoService;
import com.hongguan.oms.service.grid.GridGroupCustInfoService;
import com.hongguan.oms.service.grid.GridSchoolInfoService;
import com.hongguan.oms.service.grid.GridUnitService;
import com.hongguan.oms.utils.Util;
import com.hongguan.oms.web.BaseHandler;

/**
 * @author jiangting
 * @version $Id: GridSchoolInfoHandler.java 130 $
 */
public class GridSchoolInfoHandler extends BaseHandler {

	private GridSchoolInfoService gridSchoolInfoService;
	private GridUnitService gridUnitService;
	private GridAreasService gridAreasService;
	private GridCommunityInfoService gridCommunityInfoService; 
	private GisService gisService;
	private GridAuthInfoService gridAuthInfoService; 
	private GridAddressInfoService gridAddressInfoService;
	private GridGroupCustInfoService gridGroupCustInfoService;

	/**
	 * list
	 * @param request
	 * @param response
	 * @return mv
	 * @throws Exception
	 */
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = this.getSuccessModelView();
		UserInfo user = Util.getUserInfoFromSession(request);
		if (user == null)
			return this.getErrorMessageView("�޷�����û���,�����µ�¼!");
		Map<String, Object> param = new HashMap<String, Object>();
		String role_id=Util.toNull(user.getRoleId());
		if(role_id!=null){
			List unitList=new ArrayList();
			param.put("role_id", role_id);
			List<GridAuthInfo> list=new ArrayList<GridAuthInfo>();
			//����¼�˽�ɫ�����пɷ��ʿ�
			list=gridAuthInfoService.authQueryByRoleId(param);
			if(list.size()<=0&&!user.getUserId().equals("admin")){
				return mv;
			}
			String name = request.getParameter("name");
			String address = request.getParameter("address");
			Long gridId = Util.getLong(request, "regionId");
			Long type=Util.getLong(request, "type",-1);
			// С�����
			if (StringUtils.isNotBlank(name)) {
				param.put("school_name","%" + name.trim().toLowerCase() + "%");
			}
			// С���ַ
			if (StringUtils.isNotBlank(address)) {
				param.put("school_address","%" + address.trim().toLowerCase() + "%");
			}
			// ��������
			if(gridId>0){
				param.put("grid_id", gridId );
			}
			if(type>=0){
				param.put("school_type", type);
			}
			String start = request.getParameter("start");
			String limit = request.getParameter("limit");
			start = start == null ? "0" : start;
			limit = limit == null ? "15" : limit;
			int beginRow = Integer.parseInt(start);
			int endRow = Integer.parseInt(limit) + beginRow;
			param.put("beginRow", beginRow);
			param.put("endRow", endRow);
			param.put("unit_type", ConstGrid.UNIT_TYPE_SCHOOL);
			if(user.getUserId().equals("admin")){
				unitList.addAll(gridUnitService.searchUnitByParam(param));
				param.put("unitList",unitList);	
			}
			for (int i = 0; i < list.size(); i++) {
				GridAuthInfo gridAuthInfo=new GridAuthInfo();
				gridAuthInfo=list.get(i);
				long entity_id=gridAuthInfo.getEntityId();
				if(gridAuthInfo.getAuthLevel()==0){
					unitList.addAll(gridUnitService.searchUnitByParam(param));
					param.put("unitList",unitList);	
					mv.addObject("result",gridSchoolInfoService.searchSchool(param));
					mv.addObject("total", gridSchoolInfoService.searchSchoolCount(param));
					return mv;
				}
				if(gridAuthInfo.getAuthLevel()==2){//���id
					//���grid_id���unti_id�б�
					param.put("grid_id", entity_id);
				}else if(gridAuthInfo.getAuthLevel()==1){//����id
					//���area_id���unit_id�б�
					param.put("area_id", entity_id);
				}
				unitList.addAll(gridUnitService.searchUnitByParam(param));
			}
			if(unitList.size()<=0){
				return mv;
			}			
			param.put("unitList",unitList);	
			mv.addObject("result",gridSchoolInfoService.searchSchool(param));
			mv.addObject("total", gridSchoolInfoService.searchSchoolCount(param));
		}
		return mv;
	}
	public ModelAndView prepare(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = this.getSuccessModelView();
		Map<String, Object> param = new HashMap<String, Object>();
		String school_id=request.getParameter("school_id");//1
		String unit_manager=request.getParameter("unit_manager");//14311
		String address_id=request.getParameter("address_id");//55
		String gis_id=request.getParameter("gis_id");//1
		String unit_id=request.getParameter("unit_id");//2
		String contact_id=request.getParameter("contact_id");
		param.put("school_id",school_id );
		param.put("user_id",unit_manager);
		param.put("address_id", address_id);
		param.put("gis_id", gis_id);
		param.put("unit_id",unit_id);
		param.put("contact_id",contact_id);
		
		mv.addObject("school", gridSchoolInfoService.searchSchoolById(param));

		mv.addObject("address",gridAddressInfoService.searchAddressById(param));
		mv.addObject("gis", gisService.searchGisById(param));

		mv.addObject("unit_manager", gridCommunityInfoService.searchUnitManagerById(param));//user_id,�ͻ�����
		mv.addObject("unit", gridCommunityInfoService.searchUnitById(param));
		
		param.put("entity_id",school_id );
		param.put("entity_type", ConstGrid.UNIT_TYPE_SCHOOL);
		mv.addObject("main_contact",gridCommunityInfoService.searchContactById(param));
		return mv;
	}

	/**
	 * save
	 * 
	 * @param request
	 * @param response
	 * @return mv
	 * @throws Exception
	 */
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = this.getSuccessModelView();
		Map<String, Object> param = new HashMap<String, Object>();
		Long com_id = 0L;//����Ӫ��ԪID
		String school_id=request.getParameter("school_id");
		
		if(StringUtils.isNotBlank(school_id)){
			param.clear();
			com_id = NumberUtils.toLong(request.getParameter("unit_id"));
			param=this.getPrepareInfo(request);
			//�ж��Ƿ�ͬ��
			//boolean isNameSame=gridSchoolInfoService.isNameSame(param);
			//if(isNameSame){
			//	return this.getErrorMessageView("�����������Ѵ���,����������!");
			//}
			param.put("entity_type",ConstGrid.UNIT_TYPE_SCHOOL);
			param.put("entity_id",school_id);
			
			gridSchoolInfoService.modifySchool(param);
			gridUnitService.modifyUnit(param);
			gridAddressInfoService.modifyAddress(param);
			gisService.modifyGis(param);
			
			param.put("contact_type",ConstGrid.CONTACT_TYPE_SCHOOL_CONTACT);
			param.put("contact_id", Util.toNull(request.getParameter("contact_id")));
			param.put("user_name", Util.toNull(request.getParameter("contact_name")));
			param.put("phone_num_office", Util.toNull(request.getParameter("contact_phone")));
			gridCommunityInfoService.modifyContact(param);
			param.clear();
		}else if(StringUtils.isNotBlank(school_id)==false){
			param.clear();
			param=this.getPrepareInfo(request);
//			�ж��Ƿ�ͬ��
			boolean isNameSame=gridSchoolInfoService.isNameSame(param);
			if(isNameSame){
				return this.getErrorMessageView("�����������Ѵ���,����������!");
			}
			long gis_id=gisService.addGis(param);			
			long address_id=gridAddressInfoService.addAddress(param);
			param.put("address_id", address_id);
			param.put("gis_id", gis_id);
			
			param.put("unit_type", ConstGrid.UNIT_TYPE_SCHOOL);
			
			long unit_id=gridUnitService.addUnit(param);
			com_id = unit_id;
			param.put("unit_id", unit_id);
			
			
			long schoolId=gridSchoolInfoService.addSchool(param);
			param.put("school_id", schoolId);
			
			param.put("entity_type",ConstGrid.UNIT_TYPE_SCHOOL);
			param.put("entity_id",schoolId);
			
			param.put("contact_type",ConstGrid.CONTACT_TYPE_SCHOOL_CONTACT);
			param.put("user_name", Util.toNull(request.getParameter("contact_name")));
			param.put("phone_num_office", Util.toNull(request.getParameter("contact_phone")));
			long contact_id= gridCommunityInfoService.addContact(param);
			//��contact_id��ӽ�ȥ
			param.put("main_contact", contact_id);
			gridSchoolInfoService.modifySchool(param);
			param.clear();
		}
		//���ſͻ�������ĳ��������
		String groupCustInfo = request.getParameter("groupCustInfoID");
		String[] custId = StringUtils.split(groupCustInfo,"|");
		Long cid = 0L;
		if(custId.length > 0){
			for(String id : custId){
				cid = NumberUtils.toLong(id);
				if(cid>0){
					GridGroupCustInfo gridGroupCustInfo = new GridGroupCustInfo();
					gridGroupCustInfo.setCustId(cid);
					gridGroupCustInfo.setGridId(com_id);
					gridGroupCustInfoService.updateByPrimaryKeySelective(gridGroupCustInfo);
				}
			}
		}
		return mv;
	}

	/**
	 * delete
	 * 
	 * @param request
	 * @param response
	 * @return mv
	 * @throws Exception
	 */
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		Map<String, Object> param = new HashMap<String, Object>();
		String school_id=request.getParameter("school_id");
		String gis_id =request.getParameter("gis_id");
		String address_id =request.getParameter("address_id");
		String unit_id =request.getParameter("unit_id");
		param.put("entity_id", Util.toNull(school_id));
		param.put("entity_type", ConstGrid.UNIT_TYPE_SCHOOL);
		
		param.put("gis_id", Util.toNull(gis_id));
		param.put("address_id", Util.toNull(address_id));
		param.put("unit_id", Util.toNull(unit_id));
		param.put("school_id",school_id);
		gridSchoolInfoService.deleteSchool(param);
		gridCommunityInfoService.deleteAddress(param);
		gridCommunityInfoService.deleteContact(param);
		gridCommunityInfoService.deleteGis(param);
		gridCommunityInfoService.deleteUnit(param);
		mv=this.getSuccessModelView();
		return mv;
	}
	/**
	 * getSchoolInfo
	 * @param request
	 * @return
	 */
	public Map getPrepareInfo(HttpServletRequest request) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("school_id", Util.toNull(request.getParameter("school_id")));
		param.put("grid_id", Util.toNull(request.getParameter("grid_id")));
		param.put("unit_id", Util.toNull(request.getParameter("unit_id")));
		param.put("school_name", Util.toNull(request.getParameter("school_name")));
		param.put("school_type", Util.toNull(request.getParameter("school_type")));
		param.put("teachers_num", Util.toNull(request.getParameter("teachers_num")));
		param.put("students_num", Util.toNull(request.getParameter("students_num")));
		param.put("contact_id", Util.toNull(request.getParameter("contact_id")));
		param.put("contact_name", Util.toNull(request.getParameter("contact_name")));
		param.put("phone_num_office", Util.toNull(request.getParameter("contact_phone")));
		param.put("school_char", Util.toNull(request.getParameter("school_char")));
		
		param.put("unit_id", Util.toNull(request.getParameter("unit_id")));
		param.put("grid_id", Util.toNull(request.getParameter("grid_id")));
		param.put("area_id", Util.toNull(request.getParameter("area_id")));
		param.put("unit_type", ConstGrid.UNIT_TYPE_SCHOOL);
		param.put("entity_type", ConstGrid.UNIT_TYPE_SCHOOL);
		param.put("unit_name", Util.toNull(request.getParameter("school_name")));
		
		param.put("unit_manager", Util.toNull(request.getParameter("unit_manager_user_id")));
		param.put("user_id", Util.toNull(request.getParameter("unit_manager_user_id")));//��tap_c_user_info�в���
		param.put("user_name", Util.toNull(request.getParameter("unit_manager_user_name")));
		param.put("user_phone", Util.toNull(request.getParameter("unit_manager_user_phone")));
		
		param.put("address_id", Util.toNull(request.getParameter("address_id")));
		String district=request.getParameter("district");
		String neighbourhood=request.getParameter("neighbourhood");
		String street=request.getParameter("street");
		String alley=request.getParameter("alley");
		String door_num=request.getParameter("door_num");
		
		String address_detail= gridCommunityInfoService.getAddressDetail(district, neighbourhood, street, alley, door_num);
		param.put("address_detail", address_detail);
		
		param.put("district", Util.toNull(district));
		param.put("neighbourhood", Util.toNull(neighbourhood));
		param.put("street", Util.toNull(street));
		param.put("alley", Util.toNull(alley));
		param.put("door_num", Util.toNull(door_num));
		
		param.put("gis_id", Util.toNull(request.getParameter("gis_id")));//gis_info��
		param.put("icon_id", Util.toNull(request.getParameter("icon_id")));
		param.put("type", ConstGrid.GIS_TYPE_AREA);
		String coords_info=request.getParameter("coords_info");
		if(StringUtils.isNotBlank(coords_info)){
			Map map=gridCommunityInfoService.setNamePosAndRectScale(coords_info);
			String namepos=map.get("namepos").toString();
			String rectScale=map.get("rectScale").toString();
			param.put("name_pos", namepos);
			param.put("rect_scale", rectScale);
		}
		param.put("coords_info", Util.toNull(coords_info));
		param.put("line_color", Util.toNull(request.getParameter("line_color")));
		param.put("main_fill_color", Util.toNull(request.getParameter("main_fill_color")));
		return param;
	}


	public void setGridUnitService(GridUnitService gridUnitService) {
		this.gridUnitService = gridUnitService;
	}
	public void setGridSchoolInfoService(GridSchoolInfoService gridSchoolInfoService) {
		this.gridSchoolInfoService = gridSchoolInfoService;
	}

	public void setGisService(GisService gisService) {
		this.gisService = gisService;
	}

	public void setGridAddressInfoService(
			GridAddressInfoService gridAddressInfoService) {
		this.gridAddressInfoService = gridAddressInfoService;
	}


	public void setGridAreasService(GridAreasService gridAreaService) {
		this.gridAreasService = gridAreaService;
	}
	public void setGridCommunityInfoService(
			GridCommunityInfoService gridCommunityInfoService) {
		this.gridCommunityInfoService = gridCommunityInfoService;
	}
	public void setGridGroupCustInfoService(
			GridGroupCustInfoService gridGroupCustInfoService) {
		this.gridGroupCustInfoService = gridGroupCustInfoService;
	}
	public void setGridAuthInfoService(GridAuthInfoService gridAuthInfoService) {
		this.gridAuthInfoService = gridAuthInfoService;
	}
}
